#include "StdAfx.h"
#include "Sequence.h"
#include <iostream>
#include <fstream>


CSequence::CSequence(void)
{
	pSeq = 0;
}

CSequence::~CSequence(void)
{
	//SefDestructor();
}

void CSequence::Init( tagSeqInfo& seqInfo )
{
	this->strSeqName = seqInfo.strSeqName;
	this->strSeqPath = seqInfo.strSeqPath;
	this->strBeginTime = seqInfo.strBeginTime;
	this->strInterval = seqInfo.strInterval;
	this->Code=seqInfo.SeqCode;
	this->pLogic=seqInfo.plugin;
}

std::wstring c2w(const char *pc)  
{  
	std::wstring val = L"";  

	if(NULL == pc)  
	{  
		return val;  
	}  
	//size_t size_of_ch = strlen(pc)*sizeof(char);  
	//size_t size_of_wc = get_wchar_size(pc);  
	size_t size_of_wc;  
	size_t destlen = mbstowcs(0,pc,0);  
	if (destlen ==(size_t)(-1))  
	{  
		return val;  
	}  
	size_of_wc = destlen+1;  
	wchar_t * pw  = new wchar_t[size_of_wc];  
	mbstowcs(pw,pc,size_of_wc);  
	val = pw;  
	delete pw;  
	return val;  
}  

bool CSequence::LoadFile()
{
	//wCstring Code;
	if(Code.length()<=0)
	{
		ifstream in(strSeqPath.c_str(), ios::in);
		istreambuf_iterator<char> beg(in), end;
		string strdata(beg, end);
		in.close();
		Code=c2w(strdata.c_str());
	}
	aScript.InstallPlugin(pLogic);
	aError=aScript.Load(Code);
	if(aError.Position)
	{
		wcout<<aError.Message<<endl;
		wcout<<aError.Position<<endl;
		return false;
	}
	else
	{		
		
	}
	return true;
}



bool CSequence::SefDestructor()
{
	if(pSeq)
		delete [] pSeq;
	pLogic->SefDestructor();
	return true;
}
bool CSequence::InitStart()
{
	//pLogic->InitStart();
	return true;
}

bool CSequence::Run()
{
	Environment aEnvironment;
	/*aEnvironment.SetVariable(L"var1", L"123");*/
	aError=aScript.Run(aEnvironment);
	if(aError.Position)
	{
		wcout<<aError.Message<<endl;
		wcout<<aError.Position<<endl;
		return false;
	}
	return true;
}


bool CSequence::CanStart()
{
	return true;
}

